home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 03 - Advanced Graphics / Example 6 / main.c < prev    next >
C/C++ Source or Header  |  1995-02-28  |  5KB  |  239 lines

  1. //
  2. //    File: main.c
  3. //
  4. //    This file contains the code for a VERY minimal application.
  5. //    It will bring up a set an app palette, open a window and
  6. //     run a sprite demo. 
  7. //
  8. //    2/18/95 -- Created by Mick
  9. //
  10.  
  11. // include files
  12.  
  13. #include <profiler.h>
  14.  
  15. #include "global.h"
  16.  
  17. #include <LowMem.h>        // the CodeWarrior version -- if you are using Symantec, include LoMem.h instead
  18. #include <Palettes.h>
  19.  
  20. #include "main.h"
  21.  
  22. #include "demo.h"
  23.  
  24. // defines for this file
  25.  
  26. // global function declarations
  27.  
  28. void main( void );
  29.  
  30. // global data owned by this file
  31.  
  32. WindowPtr gMainWindow;                    // the window that we are drawing to
  33. CTabHandle gAppColorTable;            // the color table that we are drawing with
  34.  
  35. // local function declarations
  36.  
  37. static void initMacToolboxes( void );
  38. static void doEventLoop( void );
  39. static void hideMenuBar( void );
  40. static void showMenuBar( void );
  41.  
  42. // static data
  43.  
  44. static unsigned short sMenuBarHeight;            // the height of the menu bar when the program started
  45. static RgnHandle sOriginalGrayRgn;                                // the gray region when the program started
  46.  
  47. // functions
  48.  
  49. //
  50. // main -
  51. //        This is where it all begins.
  52. //
  53.  
  54. void main( void )
  55. {
  56.     PaletteHandle appPalette;            // the palette that we will make the application palette
  57.     
  58.     // startup the profiler
  59. #if __profile__
  60.     if ( ProfilerInit( collectDetailed, bestTimeBase, 100, 10 ) != noErr )
  61.         {
  62.             Debugger();
  63.         }
  64. #endif
  65.  
  66.     // fire up the toolboxes
  67.     initMacToolboxes();
  68.     
  69.     // get the color table and make it the default palette
  70.     gAppColorTable = GetCTable( kAppColorTableResID );
  71.     appPalette = NewPalette( 256, gAppColorTable, pmExplicit + pmTolerant, 0 );
  72.     SetPalette( ( WindowPtr )-1L,    appPalette,    kFalse    );
  73.     
  74.     // hide the menu bar
  75.     hideMenuBar();
  76.     
  77.     // create and show the window
  78.     gMainWindow = GetNewCWindow( kMainWindowResID, ( Ptr )kNil, kWindowInFront );
  79.     ShowWindow( gMainWindow );
  80.     SetPort( gMainWindow );
  81.  
  82.     // give the demo code a chance to setup
  83.     startupDemo();
  84.     
  85.     // handle events until the user signals a quit
  86.     doEventLoop();
  87.     
  88.     // give the demo code a chance to shutdown
  89.     shutdownDemo();
  90.     
  91.     // hide and delete the window
  92.     HideWindow( gMainWindow );
  93.     DisposeWindow( gMainWindow );
  94.     
  95.     // show the menu bar again
  96.     showMenuBar();
  97.  
  98.     // shutdown the profiler
  99. #if __profile__
  100.     ProfilerDump( "\pDemo.prof" );
  101.     ProfilerTerm();
  102. #endif
  103. }
  104.  
  105. //
  106. // initMacToolboxes -
  107. //        Initialize all the toolboxes that we will need.
  108. //
  109.  
  110. void initMacToolboxes( void )
  111. {
  112.     // initialize all the toolboxes
  113.     InitGraf( &( qd.thePort ) );
  114.     InitFonts();
  115.     InitWindows();
  116.     InitMenus();
  117.     TEInit();
  118.     InitDialogs( ( ProcPtr )kNil );
  119.     InitCursor();
  120.     
  121.     // make sure that we have the enitre heap
  122.     MaxApplZone();
  123.     
  124.     // allocate some extra master pointers
  125.     MoreMasters();
  126.     MoreMasters();
  127.     MoreMasters();
  128.     MoreMasters();
  129.     
  130.     // set the random seed
  131.     qd.randSeed = TickCount();
  132. }
  133.  
  134.  
  135. //
  136. //    doEventLoop -
  137. //
  138. //    Get and process events until the user signals a quit.
  139. //
  140.  
  141. void doEventLoop( void )
  142. {
  143.     unsigned char quitNow;            // a flag that notes if the user has signaled a quit
  144.     EventRecord theEvent;                    // a place to put an event
  145.     
  146.     quitNow = kFalse;
  147.     while( !quitNow )
  148.         {
  149.             if( WaitNextEvent( everyEvent, &theEvent, 0L, ( RgnHandle )kNil ) == kTrue )
  150.                 {
  151.                     switch( theEvent.what )
  152.                         {
  153.                             case mouseDown:
  154.                                 // give the demo uninteruppted time to draw
  155.                                 while( StillDown() )
  156.                                     {
  157.                                         doDemoFrame();
  158.                                     }
  159.                                 break;
  160.                             case keyDown:
  161.                                 // check to see if the user has hit cmd-q (to signal a quit)
  162.                                 if ( ( theEvent.modifiers & cmdKey ) && ( ( theEvent.message & charCodeMask ) == 'q' ) )
  163.                                     {
  164.                                         quitNow = kTrue;
  165.                                     }
  166.                                 else
  167.                                     {
  168.                                         // send the key to the demo code
  169.                                         demoKey( ( unsigned char )( theEvent.message & charCodeMask ) );
  170.                                     }
  171.                                 break;
  172.                             case updateEvt:
  173.                                 BeginUpdate( gMainWindow );
  174.                                 EndUpdate( gMainWindow );
  175.                             default:
  176.                                 break;
  177.                         }
  178.                 }
  179.             else
  180.                 {
  181.                     // give the demo code some time to run
  182.                     doDemoFrame();
  183.                 }
  184.         }
  185. }
  186.  
  187.  
  188. //
  189. //    hideMenuBar -
  190. //
  191. //    Hides the menu bar and adds it to the area that can be drawn
  192. //
  193.  
  194. void hideMenuBar( void )
  195. {
  196.     GDHandle mainScreen;                    // the information on the main screen
  197.     Rect mainScreenRect;                            // the rect that bounds the menu bar
  198.     RgnHandle mainScreenRgn;                // the region of the menu bar
  199.  
  200.     // record the menu bar height
  201.     sMenuBarHeight = GetMBarHeight();
  202.  
  203.     // set the menu bar to no height
  204.     LMSetMBarHeight( 0 );
  205.     
  206.     // save the original gray regions (so we can restore it later)
  207.     sOriginalGrayRgn = NewRgn();
  208.     CopyRgn( GetGrayRgn(), sOriginalGrayRgn );
  209.  
  210.     // make sure that the entire main screen is ours to play with (no menu bars or corners)
  211.     mainScreenRgn = NewRgn();
  212.     mainScreen = GetMainDevice();
  213.     mainScreenRect = ( ( *mainScreen )->gdRect );
  214.     RectRgn( mainScreenRgn, &mainScreenRect );
  215.     UnionRgn( sOriginalGrayRgn, mainScreenRgn, GetGrayRgn() );
  216.     
  217.     // draw the desktop
  218.     PaintOne( ( WindowRef )kNil, sOriginalGrayRgn );
  219. }
  220.  
  221.  
  222. //
  223. //    showMenuBar -
  224. //
  225. //    Shows the menu bar and removes it from the drawable area.
  226. //
  227.  
  228. void showMenuBar( void )
  229. {
  230.     // set the menu bar to its normal height
  231.     LMSetMBarHeight( sMenuBarHeight );
  232.     
  233.     // restore the original gray region
  234.     SectRgn( sOriginalGrayRgn, GetGrayRgn(), GetGrayRgn() );
  235.     
  236.     // draw the menu bar
  237.     DrawMenuBar();
  238. }
  239.